package com.library.common.util;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class SdCardUtil {

    public final String CACHE_PATH;
    public final String PROJECT_FILE_PATH;
    public final String DEFAULT_PHOTO_PATH;
    public final String DEFAULT_RECORD_PATH;
    public final String DEFAULT_VIDEO_PATH;
    public final String DEFAULT_DOWNLOAD_PATH;

    public String getCACHE_PATH() {
        return CACHE_PATH;
    }

    public String getPROJECT_FILE_PATH() {
        return PROJECT_FILE_PATH;
    }

    public String getDEFAULT_PHOTO_PATH() {
        return DEFAULT_PHOTO_PATH;
    }

    public String getDEFAULT_RECORD_PATH() {
        return DEFAULT_RECORD_PATH;
    }

    public String getDEFAULT_VIDEO_PATH() {
        return DEFAULT_VIDEO_PATH;
    }

    public String getDEFAULT_DOWNLOAD_PATH() {
        return DEFAULT_DOWNLOAD_PATH;
    }

    private static SdCardUtil sInstance;
    public static SdCardUtil get(Context mContext) {
        if (sInstance == null) {
            synchronized (SdCardUtil.class) {
                if (sInstance == null) sInstance = new SdCardUtil();
            }
        }
        return sInstance;
    }

    private SdCardUtil() {
        this.PROJECT_FILE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Malaysia";
        this.DEFAULT_PHOTO_PATH = PROJECT_FILE_PATH + File.separator + "Images";
        this.CACHE_PATH = PROJECT_FILE_PATH + File.separator + "Cache";
        this.DEFAULT_RECORD_PATH = PROJECT_FILE_PATH + File.separator + "Record";
        this.DEFAULT_VIDEO_PATH = PROJECT_FILE_PATH + File.separator + "Video";
        this.DEFAULT_DOWNLOAD_PATH = PROJECT_FILE_PATH + File.separator + "Download";
    }

    /**
     * 初始化文件目录
     */
    public void initFileDir() {
        createFolder(PROJECT_FILE_PATH);
        createFolder(DEFAULT_PHOTO_PATH);
        createFolder(DEFAULT_RECORD_PATH);
        createFolder(DEFAULT_VIDEO_PATH);
        createFolder(DEFAULT_DOWNLOAD_PATH);
        createFolder(CACHE_PATH);
    }

    public boolean createFolder(String folderPath) {
        if (!TextUtils.isEmpty(folderPath)) {
            File folder = new File(folderPath);
            return createFolder(folder);
        }
        return false;
    }

    /**
     * Create a folder, If the folder exists is not created.
     *
     * @param targetFolder folder path.
     * @return True: success, or false: failure.
     */
    public boolean createFolder(File targetFolder) {
        if (targetFolder.exists()) {
            if (targetFolder.isDirectory())
                return true;
            //noinspection ResultOfMethodCallIgnored
            targetFolder.delete();
        }
        return targetFolder.mkdirs();
    }


    /**
     * 写文件到SD卡
     *
     * @param str
     */
    public static void writeTxt(String str) {
        //新建文件夹
        String folderName = "malaysia";
        File sdCardDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), folderName);
        if (!sdCardDir.exists()) {
            if (!sdCardDir.mkdirs()) {
                try {
                    sdCardDir.createNewFile();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            //新建文件
            File saveFile = new File(sdCardDir, "user.txt");

            if (!saveFile.exists()) {
                saveFile.createNewFile();
            }
            final FileOutputStream outStream = new FileOutputStream(saveFile);

            try {
                outStream.write(str.getBytes());
                outStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //outStream.write("测试写入文件".getBytes());
            //outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
